﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameNode
{
    class SmartNode : ConsoleNode
    {
        private bool maxUp;
        private bool maxLeft;
        private bool direction; //false - moving right; true = moving left

        public SmartNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor) : base(x, y, nodeSymbol, nodeColor)
        {

        }

        public override void Move(Func<int, int, Position> positionStatus)
        {
            if (!maxUp)
            {
                if (positionStatus(X, Y - 1) == Position.Free)
                    MoveUp();
                else if (positionStatus(X, Y - 1) == Position.Border)
                    maxUp = true;
            }
            else if (!maxLeft)
            {
                if (positionStatus(X - 1, Y) == Position.Free)
                    MoveLeft();
                else if (positionStatus(X - 1, Y) == Position.Border)
                    maxLeft = true;
            }
            else if (maxUp && maxLeft)
            {
                if (!direction)
                {
                    if (positionStatus(X + 1, Y) == Position.Free)
                        MoveRight();
                    else if (positionStatus(X + 1, Y) == Position.Border)
                    {
                        if (positionStatus(X, Y + 1) == Position.Free)
                        {
                            MoveDown();
                            direction = !direction;
                        }
                    }
                }
                else if (direction)
                {
                    if (positionStatus(X - 1, Y) == Position.Free)
                        MoveLeft();
                    else if (positionStatus(X - 1, Y) == Position.Border)
                    {
                        if (positionStatus(X, Y + 1) == Position.Free)
                        {
                            MoveDown();
                            direction = !direction;
                        }
                    }

                }

            }

        }

    }
}
